var gulp = require("gulp");
var cssnano = require("gulp-cssnano");
var cache = require("gulp-cache");
var concat = require("gulp-concat");
var imagemin = require("gulp-imagemin");
var rename = require("gulp-rename");
var uglify = require("gulp-uglify");
var bs = require("browser-sync").create();
var sass = require("gulp-sass");

var path = {
	'css':'./src/css/',
	'js':'./src/js/',
	'images':'./src/images',
	'css_dist':'./dist/css/',
	'js_dist':'./dist/js/',
	'images_dist':'./dist/images/',
	'html':'./templates/**/'
};

//处理html的任务
gulp.task('html',function () {
	gulp.src(path.html + '*.html')
		.pipe(bs.stream())
})

//定义处理一个css的任务
gulp.task('css',function () {
	gulp.src(path.css + '*.scss')
		.pipe(sass().on("error",sass.logError))
		.pipe(cssnano())
		.pipe(rename({"suffix":".min"}))
		.pipe(gulp.dest(path.css_dist))
		.pipe(bs.stream())
});

//定义处理js文件的任务
gulp.task('js',function () {
	gulp.src(path.js + '*.js')
		.pipe(uglify())
		.pipe(rename({"suffix":".min"}))
		.pipe(gulp.dest(path.js_dist))
		.pipe(bs.stream())
});

//定义处理图片文件的任务
gulp.task('images',function () {
	gulp.src(path.images + '*.*')
		.pipe(cache(imagemin()))
		.pipe(gulp.dest(path.images_dist))
		.pipe(bs.stream())
});

//定义监听文件修改的任务
gulp.task('watch',function () {
	gulp.watch(path.css + '*.scss',['css']);
	gulp.watch(path.js + '*.js',['js']);
	gulp.watch(path.images + '*.*',['images']);
	gulp.watch(path.html + '*.html',['html']);
});

//初始化browser-sync的任务
gulp.task('bs',function () {
	bs.init({
		'server':{
			'baseDir':'./'
		}
	});
});

//创建一个默认任务
gulp.task('default',['bs','watch']);